import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect:'/login'
    },
    {
      path: '/login',
      component: () => import('@/views/login')
    },
    {
      path: '/main',
      component: () => import('@/views/main'),
      children:[
        {
          path:'/main/home',
          component:() => import('@/views/main/pages/Home.vue'),
        }, {
          path:'/main/user',
          component:() => import('@/views/main/pages/User.vue'),
        }, {
          path:'/main/goods',
          component:() => import('@/views/main/pages/Goods.vue'),
        }, {
          path:'/main/task',
          component:() => import('@/views/main/pages/Task.vue'),
        }, {
          path:'/main/car',
          component:() => import('@/views/main/pages/Car.vue'),
        }, {
          path:'/main/courier',
          component:() => import('@/views/main/pages/Courier.vue'),
        }, {
          path:'/main/swiper',
          component:() => import('@/views/main/pages/Swiper.vue'),
        },
      ]
    },
  ]

})

// 路由拦截，判断是否需要登录
router.beforeEach((to, from, next) => {
    const user = JSON.parse(sessionStorage.getItem('user'))
    if (!user && to.path !== '/login') {
      next({
        path: '/',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
})

export default router
